﻿using System;
using System.Web.Security;
using Jayden.Dll.Web.Events;
using Jayden.Dll.Web.HtmlControls;

namespace Jayden.Dll.Web.Controls.Admin
{
	public class RoleProviderDiv : Div
	{
		public delegate void OnRolesCreated(HttpHandlerContext context, string[] roles);
		public delegate void OnRolesDeleted(HttpHandlerContext context, string[] roles);
		public delegate void OnUsersAdddedToRoles(HttpHandlerContext context, string[] users, string[] roles);
		public delegate void OnUsersRemovedFromRoles(HttpHandlerContext context, string[] users, string[] roles);
		public delegate void OnRolesNotCreated(HttpHandlerContext context, string[] roles);
		public delegate void OnRolesNotDeleted(HttpHandlerContext context, string[] roles);
		public delegate void OnUsersNotAdddedToRoles(HttpHandlerContext context, string[] users, string[] roles);
		public delegate void OnUsersNotRemovedFromRoles(HttpHandlerContext context, string[] users, string[] roles);

		public class RoleCommandHandler : HttpCommandHandler
		{
			public RoleCommandHandler() : this("CreateRoles", "DeleteRoles", "AddUsersToRoles", "RemoveUsersFromRoles") { }
			public RoleCommandHandler(string createCommand, string deleteCommand, string addUsersToRolesCommand, string removeUsersFromRolesCommand)
				: base(createCommand, deleteCommand, addUsersToRolesCommand, removeUsersFromRolesCommand)
			{
				RegisterEvent("RolesCreated");
				RegisterEvent("RolesDeleted");
				RegisterEvent("UsersAddedToRoles");
				RegisterEvent("UsersRemovedFromRoles");

				RegisterEvent("RolesNotCreated");
				RegisterEvent("RolesNotDeleted");
				RegisterEvent("UsersNotAddedToRoles");
				RegisterEvent("UsersNotRemovedFromRoles");
			}
			public event OnRolesCreated RolesCreated { add { AddEvent<OnRolesCreated>("RolesCreated", value); } remove { RemoveEvent<OnRolesCreated>("RolesCreated", value); } }
			public event OnRolesDeleted RolesDeleted { add { AddEvent<OnRolesDeleted>("RolesDeleted", value); } remove { RemoveEvent<OnRolesDeleted>("RolesDeleted", value); } }
			public event OnUsersAdddedToRoles UsersAddedToRoles { add { AddEvent<OnUsersAdddedToRoles>("UsersAddedToRoles", value); } remove { RemoveEvent<OnUsersAdddedToRoles>("UsersAddedToRoles", value); } }
			public event OnUsersRemovedFromRoles UsersRemovedFromRoles { add { AddEvent<OnUsersRemovedFromRoles>("UsersRemovedFromRoles", value); } remove { RemoveEvent<OnUsersRemovedFromRoles>("UsersRemovedFromRoles", value); } }

			public event OnRolesNotCreated RolesNotCreated { add { AddEvent<OnRolesNotCreated>("RolesNotCreated", value); } remove { RemoveEvent<OnRolesNotCreated>("RolesNotCreated", value); } }
			public event OnRolesNotDeleted RolesNotDeleted { add { AddEvent<OnRolesNotDeleted>("RolesNotDeleted", value); } remove { RemoveEvent<OnRolesNotDeleted>("RolesNotDeleted", value); } }
			public event OnUsersNotAdddedToRoles UsersNotAddedToRoles { add { AddEvent<OnUsersNotAdddedToRoles>("UsersNotAddedToRoles", value); } remove { RemoveEvent<OnUsersNotAdddedToRoles>("UsersNotAddedToRoles", value); } }
			public event OnUsersNotRemovedFromRoles UsersNotRemovedFromRoles { add { AddEvent<OnUsersNotRemovedFromRoles>("UsersNotRemovedFromRoles", value); } remove { RemoveEvent<OnUsersNotRemovedFromRoles>("UsersNotRemovedFromRoles", value); } }

			protected override void ProcessCommand(HttpHandlerContext context, string command)
			{
				if (command == "CreateRoles")
				{
					string[] roles = context.Request["roles"].Split(',');
					try
					{
						foreach (string roleName in roles)
							Roles.CreateRole(roleName);
						Invoke("RolesCreated", context, roles);
					}
					catch (Exception e)
					{
						Invoke("RolesNotCreated", context, roles, e);
					}
				}
				else if (command == "DeleteRoles")
				{
					string[] roles = context.Request["roles"].Split(',');
					try
					{
						foreach (string roleName in roles)
							Roles.DeleteRole(roleName);
						Invoke("RolesDeleted", context, roles);
					}
					catch (Exception e)
					{
						Invoke("RolesNotDeleted", context, roles, e);
					}
				}
				else if (command == "AddUsersToRoles")
				{
					string queryRoles = context.Request["rolesToAdd"];
					if (queryRoles != null)
					{
						string[] roleNames = queryRoles.Split(',');
						string[] usernames = context.Request["users"].Split(',');
						try
						{
							Roles.AddUsersToRoles(usernames, roleNames);
							Invoke("UsersAddedToRoles", context, usernames, roleNames);
						}
						catch (Exception e)
						{
							Invoke("UsersNotAddedToRoles", context, usernames, roleNames);
						}
					}
				}
				else if (command == "RemoveUsersFromRoles")
				{
					string queryRoles = context.Request["rolesToRemove"];
					if (queryRoles != null)
					{
						string[] roleNames = queryRoles.Split(',');
						string[] usernames = context.Request["users"].Split(',');
						try
						{
							Roles.RemoveUsersFromRoles(usernames, roleNames);
							Invoke("UsersRemovedFromRoles", context, usernames, roleNames);
						}
						catch (Exception e)
						{
							Invoke("UsersNotRemovedFromRoles", context, usernames, roleNames);
						}
					}
				}
			}
		}

	}
}
